package com.yangyu.util;

import org.springframework.mail.MailException;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.util.Date;

public class SendEmailUtil {

    /**
     * 发送邮件
     *
     * @param mailSender
     */
    public static void sendEmail(JavaMailSenderImpl mailSender, String email, int code) throws MailException {

        SimpleMailMessage mailMessage = new SimpleMailMessage();
        // 设置主题
        mailMessage.setSubject("动物保护协会网站注册验证码");
        // 设置内容
        mailMessage.setText("您好，您的验证码是：" + code + "， 请在五分钟内完成注册。");
        // 设置发送对象
        mailMessage.setTo(email);
        mailMessage.setFrom("1312175484@qq.com");
        // 发送
        mailSender.send(mailMessage);
    }

    /**
     * 计算两个日期间的天数
     *
     * @return 天数
     */
    public static int daysBetweenData() {

        Date date = new Date(System.currentTimeMillis());                       //获取当前日期
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        String format1 = format.format(date);
        String[] split = format1.split("-");
        int year = Integer.valueOf(split[0]);
        int month = Integer.valueOf(split[1]);
        int day = Integer.valueOf(split[2]);

        LocalDate start = LocalDate.of(2017, 10, 3);
        LocalDate end = LocalDate.of(year, month, day);

        return (int) (end.toEpochDay() - start.toEpochDay());
    }

}
